package com.kfh.ybracelet.location;

import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.utils.LogUtil;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * 邀请码页面
 * Created by Aaron on 2015/7/24.
 */
public class InviteActivity extends BaseActivity {

    //    private String id;
    private TextView tv_invite;
    private String inviteCode;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_invite);

        ShareSDK.initSDK(this);

        initWidget();

        if (CommonUtils.isNull(sp.getString(Constant.CURRENTCHILDID, ""))) {
            showToast(getResources().getString(R.string.please_choose_old_first));
            return;
        }

        getInviteKey();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);

        mApplication.cancelPendingRequests(getClass().getName());
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_invite = (TextView) findViewById(R.id.tv_invite);
        Button btn_wechat_invite = (Button) findViewById(R.id.btn_wechat_invite);

        tv_title.setText(getResources().getString(R.string.invite_baby));

        iv_left.setOnClickListener(this);
        btn_wechat_invite.setOnClickListener(this);

    }

    /**
     * 获取孩子的邀请码
     */
    private void getInviteKey() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("id", sp.getString(Constant.CURRENTCHILDID, ""));

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.GETINVITEKEY, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        if (CommonUtils.isNull(str))
                            return;

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

//                            switch (result.getInt("code")) {
//
//                                case Constant.STATUS_SUCCESS:

                                    if (CommonUtils.isNull(result.getString("inviteKey"))) {
                                        showToast("邀请码为空");
                                    } else {

                                        inviteCode = result.getString("inviteKey");
                                        tv_invite.setText("邀请码:" + inviteCode);

                                    }

//                                    break;
//                                case Constant.FA_RIGHT_ERROR:
//                                    tv_invite.setText("您的权限不够,请联系管理员");
//                                    break;
//                                default:
//                                    showToast(Constant.showErrorMsg(result.getInt("code"), InviteActivity.this));
//                                    break;
//
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:

                finish();

                break;

            case R.id.btn_wechat_invite://微信邀请

                if (!CommonUtils.isNull(inviteCode))
                    showShareDialog();
                else
                    showToast("您的权限不够,无法邀请");

                break;

        }
    }

    /**
     * 弹出分享的对话框
     * //     * @param content 分享的内容
     * //     * @param url     分享的Url
     */
    private void showShareDialog() {
        dialog = new ProgressDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.setContentView(R.layout.layout_share_friend);
//        TextView tv_wechat_circle = (TextView) dialog.findViewById(R.id.tv_wechat_circle);
        TextView tv_wechat_friend = (TextView) dialog.findViewById(R.id.tv_wechat_friend);
        //微信好友
        tv_wechat_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                share(Wechat.NAME);

            }
        });

//        //微信朋友圈
//        tv_wechat_circle.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                if (dialog!=null && dialog.isShowing()) {
//                    dialog.dismiss();
//                }
//
//                share(WechatMoments.NAME);
//
//            }
//        });
    }

    /**
     * 分享
     * //     * @param content  分享的内容
     * //     * @param url      分享的Url
     *
     * @param platform 分享的平台
     */
    private void share(String platform) {

        Platform.ShareParams psp = new Platform.ShareParams();
        psp.setTitle(getResources().getString(R.string.inv_code));

        psp.setText(getResources().getString(R.string.inv_code_1) + "  " +
                sp.getString(Constant.CURRENTCHILDNAME, "") + getResources().getString(R.string.inv_code_2) +
                inviteCode + "  " + getResources().getString(R.string.inv_code_3));//宝宝邀请码

        psp.setImageUrl("http://liaoning.sinaimg.cn/2014/1111/U10435P1195DT20141111220802.jpg");

//        psp.setImagePath("http://liaoning.sinaimg.cn/2014/1111/U10435P1195DT20141111220802.jpg");

        psp.setImageData(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

//        psp.setUrl(url);

        psp.setShareType(Wechat.SHARE_TEXT);

        Platform weChat = ShareSDK.getPlatform(platform);
        weChat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                showToast("分享完成");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                LogUtil.e("tag", "分享失败==" + throwable.getMessage());
                showToast("分享失败");
//                Toast.makeText(this, "分享失败==" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Platform platform, int i) {

            }

        });
        weChat.share(psp);
    }

}
